package dao

import (
	"go_web/models"
	"log"
)

func GetPostPage(page int, pageSize int) ([]models.Post, error) {

	page = (page - 1) * pageSize
	rows, err := DB.Query("select  * from blog_post limit ?,?", page, pageSize)
	//rows, err := DB.Query("select  * from blog_post")
	if err != nil {

		//fmt.Println("selecfrom blog_post limit ?,? err")
		return nil, err
	}

	var posts []models.Post

	for rows.Next() {
		var post models.Post
		rows.Scan(
			&post.Pid,
			&post.Title,
			&post.Content,
			&post.Markdown,
			&post.CategoryId,
			&post.UserId,
			&post.ViewCount,
			&post.Type,
			&post.Slug,
			&post.CreateAt,
			&post.UpdateAt,
		)
		//此处存在Scan扫描错误。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
		//fmt.Println("postpostpost", post)
		//if err != nil {
		//
		//	fmt.Println("Scan? err")
		//	return nil, err
		//}

		posts = append(posts, post)
	}

	//fmt.Println("posts query", posts)
	return posts, err

}

func GetPostPageBySlug(slug string, page int, pageSize int) ([]models.Post, error) {

	page = (page - 1) * pageSize
	rows, err := DB.Query("select  * from blog_post where slug=? limit ?,?", slug, page, pageSize)
	//rows, err := DB.Query("select  * from blog_post")
	if err != nil {

		//fmt.Println("selecfrom blog_post limit ?,? err")
		return nil, err
	}

	var posts []models.Post

	for rows.Next() {
		var post models.Post
		rows.Scan(
			&post.Pid,
			&post.Title,
			&post.Content,
			&post.Markdown,
			&post.CategoryId,
			&post.UserId,
			&post.ViewCount,
			&post.Type,
			&post.Slug,
			&post.CreateAt,
			&post.UpdateAt,
		)
		//此处存在Scan扫描错误。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
		//fmt.Println("postpostpost", post)
		//if err != nil {
		//
		//	fmt.Println("Scan? err")
		//	return nil, err
		//}

		posts = append(posts, post)
	}

	//fmt.Println("posts query", posts)
	return posts, err

}
func CountTotalPostPageBySlug(slug string) (count int) {
	rows := DB.QueryRow("select count(1) from blog_post where slug=?", slug)
	_ = rows.Scan(&count)
	return
}

func GetPostPageByCategoryId(cid, page, pageSize int) ([]models.Post, error) {

	page = (page - 1) * pageSize
	rows, err := DB.Query("select  * from blog_post where category_id=? limit ?,?", cid, page, pageSize)
	//rows, err := DB.Query("select  * from blog_post")
	if err != nil {

		//fmt.Println("selecfrom blog_post limit ?,? err")
		return nil, err
	}

	var posts []models.Post

	for rows.Next() {
		var post models.Post
		rows.Scan(
			&post.Pid,
			&post.Title,
			&post.Content,
			&post.Markdown,
			&post.CategoryId,
			&post.UserId,
			&post.ViewCount,
			&post.Type,
			&post.Slug,
			&post.CreateAt,
			&post.UpdateAt,
		)
		//此处存在Scan扫描错误。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
		//fmt.Println("postpostpost", post)
		//if err != nil {
		//
		//	fmt.Println("Scan? err")
		//	return nil, err
		//}

		posts = append(posts, post)
	}

	//fmt.Println("posts query", posts)
	return posts, err

}

func CountTotalPostPage() (count int) {
	rows := DB.QueryRow("select count(1) from blog_post;")
	_ = rows.Scan(&count)

	//fmt.Println(count)
	return
}

func CountTotalPostPageByCategoryID(cid int) (count int) {
	rows := DB.QueryRow("select count(1) from blog_post where  category_id=?;", cid)
	_ = rows.Scan(&count)

	//fmt.Println(count)
	return
}

func GetPostDetailByPId(pid int) (models.Post, error) {

	var post models.Post
	rows := DB.QueryRow("select * from blog_post where  pid=?;", pid)

	if rows.Err() != nil {
		return post, nil
	}

	rows.Scan(
		&post.Pid,
		&post.Title,
		&post.Content,
		&post.Markdown,
		&post.CategoryId,
		&post.UserId,
		&post.ViewCount,
		&post.Type,
		&post.Slug,
		&post.CreateAt,
		&post.UpdateAt,
	)
	//fmt.Println(post)
	return post, nil
}

func SavePost(post *models.Post) {
	ret, err := DB.Exec("insert  into  "+
		"blog_post(title,content,markdown,category_id,user_id,view_count,type,slug,creat_at,update_at)"+
		"values(?,?,?,?,?,?,?,?,?,?)",
		post.Title,
		post.Content,
		post.Markdown,
		post.CategoryId,
		post.UserId,
		post.ViewCount,
		post.Type,
		post.Slug,
		post.CreateAt,
		post.UpdateAt,
	)

	if err != nil {
		log.Println(err)
	}

	pId, _ := ret.LastInsertId()
	post.Pid = int(pId)
}
func UpdatePost(post *models.Post) {
	_, err := DB.Exec("update blog_post set title=?,content=?,markdown=?,category_id=?,view_count=?,type=?,slug=?,update_at=? where pid=?",
		post.Title,
		post.Content,
		post.Markdown,
		post.CategoryId,
		post.ViewCount,
		post.Type,
		post.Slug,
		post.UpdateAt,
		post.Pid,
	)

	if err != nil {
		log.Println(err)
	}

}

func GetAllPosts() ([]models.Post, error) {

	var posts []models.Post
	rows, err := DB.Query("select * from blog_post ")

	if err != nil {
		return posts, nil
	}

	for rows.Next() {
		var post models.Post
		rows.Scan(
			&post.Pid,
			&post.Title,
			&post.Content,
			&post.Markdown,
			&post.CategoryId,
			&post.UserId,
			&post.ViewCount,
			&post.Type,
			&post.Slug,
			&post.CreateAt,
			&post.UpdateAt,
		)

		posts = append(posts, post)
	}
	//fmt.Println(post)
	return posts, nil
}

func SearchPostByCondition(condition string) ([]models.Post, error) {

	var posts []models.Post
	rows, err := DB.Query("select * from  blog_post where  title like ?", "%"+condition+"%")
	if err != nil {
		return posts, nil
	}

	for rows.Next() {
		var post models.Post
		rows.Scan(
			&post.Pid,
			&post.Title,
			&post.Content,
			&post.Markdown,
			&post.CategoryId,
			&post.UserId,
			&post.ViewCount,
			&post.Type,
			&post.Slug,
			&post.CreateAt,
			&post.UpdateAt,
		)

		posts = append(posts, post)
	}
	//fmt.Println(post)
	return posts, nil
}
